package com.lanhe.game.tixian.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.constant.HttpStatus;
import com.lanhe.common.exception.ServiceException;
import com.lanhe.common.utils.DateUtils;
import com.lanhe.common.utils.StringUtils;

import com.lanhe.game.sysAppUser.domain.SysAppUser;
import com.lanhe.game.sysAppUser.service.ISysAppUserService;
import com.lanhe.game.tixian.domain.LhGameSq;
import com.lanhe.game.tixian.domain.LhGameTxRec;
import com.lanhe.game.tixian.mapper.LhGameSqMapper;
import com.lanhe.game.tixian.service.ILhGameSqService;
import com.lanhe.game.tixian.service.ILhGameTxRecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 用户提现申请(LhOrdersWithdrawal)表服务实现类
 *
 * @author zhanggl
 * @since 2024-01-05 21:35:20
 */
@Service
public class LhGameSqServiceImpl extends ServiceImpl<LhGameSqMapper, LhGameSq> implements ILhGameSqService {

	@Autowired
	private LhGameSqMapper lhGameSqMapper;

	@Autowired
	private ISysAppUserService iSysAppUserService;

	@Autowired
	private ILhGameTxRecService iLhGameTxRecService;

	/**
	 * 分页查询所有数据
	 *
	 * @param lhGameSq 实体
	 * @return
	 * @author zhanggl
	 * @date 2024/1/5
	 */
	@Override
	public List<LhGameSq> selectGameList(LhGameSq lhGameSq) {
		LambdaQueryWrapper<LhGameSq> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(StringUtils.isNotBlank(lhGameSq.getOrderNumber()), LhGameSq::getOrderNumber, lhGameSq.getOrderNumber());
		queryWrapper.eq(ObjectUtil.isNotNull(lhGameSq.getPayed()), LhGameSq::getPayed, lhGameSq.getPayed());
		queryWrapper.eq(ObjectUtil.isNotNull(lhGameSq.getAuditStatus()), LhGameSq::getAuditStatus, lhGameSq.getAuditStatus());
		queryWrapper.ge(ObjectUtil.isNotNull(lhGameSq.getBeginTime()), LhGameSq::getCreateTime, lhGameSq.getBeginTime());
		queryWrapper.le(ObjectUtil.isNotNull(lhGameSq.getEndTime()), LhGameSq::getCreateTime, lhGameSq.getEndTime());
		queryWrapper.eq(ObjectUtil.isNotNull(lhGameSq.getMemberId()), LhGameSq::getMemberId, lhGameSq.getMemberId());
		queryWrapper.orderByDesc(LhGameSq::getOrderId);
		return lhGameSqMapper.selectList(queryWrapper);
	}

	/**
	 * 根据ID查询订单
	 *
	 * @param orderId 订单ID
	 * @return com.lanhe.order.domain.LhOrdersWithdrawal
	 * @author zhanggl
	 * @date 2024/1/5
	 */
	@Override
	public LhGameSq selectOrdersWithdrawalById(Long orderId) {
		return lhGameSqMapper.selectById(orderId);
	}

	/**
	 * 审核
	 *
	 * @param ordersWithdrawal 实体
	 * @return int
	 * @author zhanggl
	 * @date 2024/1/5
	 */
	@Override
	@Transactional
	public int auditOrdersWithdrawalById(LhGameSq ordersWithdrawal) {

		LhGameSq lhOrdersWithdrawal = selectOrdersWithdrawalById(ordersWithdrawal.getOrderId());
		if (ObjectUtil.isNull(lhOrdersWithdrawal)) {
			throw new ServiceException("查询提现订单异常", HttpStatus.ERROR);
		}

		SysAppUser lhMemberBase = iSysAppUserService.getById(lhOrdersWithdrawal.getMemberId());
		if (ObjectUtil.isNull(lhMemberBase)) {
			throw new ServiceException("查询用户信息异常", HttpStatus.ERROR);
		}
		if (ObjectUtil.isNull(lhMemberBase.getLockAmount())){
			throw new ServiceException("锁定金额异常", HttpStatus.ERROR);
		}
		if (lhOrdersWithdrawal.getAmount().compareTo(lhMemberBase.getLockAmount()) > 0) {
			throw new ServiceException("用户锁定金额异常", HttpStatus.ERROR);
		}

		// 通过
		if (Constants.ORDERS_WITHDRAWAL_STATUS_2.equals(ordersWithdrawal.getAuditStatus())) {

			// 更新会员余额+释放锁定
			iSysAppUserService.updateMemberBaseBalance(lhOrdersWithdrawal.getMemberId(), lhOrdersWithdrawal.getAmount(), Constants.ORDERS_WITHDRAWAL_STATUS_2);


			// 增加记录
			LhGameTxRec memberWithdrawalRecords = new LhGameTxRec();
			memberWithdrawalRecords.setOrderNumber(lhOrdersWithdrawal.getOrderNumber());
			memberWithdrawalRecords.setMemberId(lhOrdersWithdrawal.getMemberId());
			memberWithdrawalRecords.setAmount(lhOrdersWithdrawal.getAmount());
			memberWithdrawalRecords.setMemberBalance(lhMemberBase.getTotalAmount());
			iLhGameTxRecService.insertWithdrawalRecord(memberWithdrawalRecords);

		} else {
			// 更新会员余额+释放锁定
			iSysAppUserService.updateMemberBaseBalance(lhOrdersWithdrawal.getMemberId(), lhOrdersWithdrawal.getAmount(), Constants.ORDERS_WITHDRAWAL_STATUS_3);
		}

		// 更新提现订单状态
		LhGameSq orders = new LhGameSq();
		orders.setOrderId(lhOrdersWithdrawal.getOrderId());
		orders.setAuditStatus(ordersWithdrawal.getAuditStatus());
		return updateOrdersWithdrawal(orders);
	}

	/**
	 * 修改
	 *
	 * @param ordersWithdrawal 实体
	 * @return int
	 * @author zhanggl
	 * @date 2024年01月05日22:30:49
	 */
	public int updateOrdersWithdrawal(LhGameSq ordersWithdrawal) {
		LambdaQueryWrapper<LhGameSq> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhGameSq::getOrderId, ordersWithdrawal.getOrderId());

		ordersWithdrawal.setAuditStatus(ordersWithdrawal.getAuditStatus());
		ordersWithdrawal.setUpdateTime(new Date());
		return lhGameSqMapper.updateById(ordersWithdrawal);
	}

	/**
	 * 设为已支付
	 *
	 * @param ordersWithdrawal 实体
	 * @return int
	 * @author zhanggl
	 * @date 2024/1/5
	 */
	@Override
	@Transactional
	public int setPayed(LhGameSq ordersWithdrawal) {
		LambdaQueryWrapper<LhGameSq> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhGameSq::getOrderId, ordersWithdrawal.getOrderId());

		ordersWithdrawal.setPayed(ordersWithdrawal.getPayed());
		ordersWithdrawal.setPayedTime(new Date());
		ordersWithdrawal.setUpdateTime(new Date());
		return lhGameSqMapper.updateById(ordersWithdrawal);
	}

	/**
	 * 提现申请
	 *
	 * @param ordersWithdrawal 实体
	 * @return int
	 * @author zhanggl
	 * @date 2024/1/5
	 */
	@Override
	@Transactional
	public int insertOrdersWithdrawa(LhGameSq ordersWithdrawal) {

		if (ordersWithdrawal.getAmount().compareTo(new BigDecimal(0.3))<0) {
			throw new ServiceException("提现余额必须大于10", HttpStatus.ERROR);
		}

		SysAppUser memberBase = iSysAppUserService.getById(ordersWithdrawal.getMemberId());
		if (ordersWithdrawal.getAmount().compareTo(memberBase.getTotalAmount())>0) {
			throw new ServiceException("余额不足,不能提现", HttpStatus.ERROR);
		}

		ordersWithdrawal.setOrderNumber(DateUtils.generateOrderCode());
		ordersWithdrawal.setWxId(memberBase.getOpenId());
		ordersWithdrawal.setZfbName(ordersWithdrawal.getZfbName());
		ordersWithdrawal.setZfbMobile(ordersWithdrawal.getZfbMobile());
		ordersWithdrawal.setAmount(ordersWithdrawal.getAmount());
		ordersWithdrawal.setAuditStatus(Constants.ORDERS_WITHDRAWAL_STATUS_1);
		ordersWithdrawal.setPayed(Constants.PAYED_NO);
		ordersWithdrawal.setCreateTime(new Date());
		lhGameSqMapper.insert(ordersWithdrawal);

		// 更新用户信息，减去余额，增加冻结金额
		return iSysAppUserService.updateLhMemberBalance(ordersWithdrawal.getMemberId(), ordersWithdrawal.getAmount());
	}
}

